package com.dormitory.dao.impl;

import com.dormitory.dao.CheckInDao;
import com.dormitory.model.CheckIn;
import com.dormitory.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CheckInDaoImpl implements CheckInDao {
    @Override
    public boolean save(CheckIn checkIn) {
        String sql = "INSERT INTO check_in_records (student_id, room_id, check_in_date, check_out_date, status) VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, checkIn.getStudentId());
            stmt.setInt(2, checkIn.getRoomId());
            stmt.setDate(3, new java.sql.Date(checkIn.getCheckInDate().getTime()));
            stmt.setDate(4, checkIn.getCheckOutDate() != null ? new java.sql.Date(checkIn.getCheckOutDate().getTime()) : null);
            stmt.setString(5, checkIn.getStatus());
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(CheckIn checkIn) {
        String sql = "UPDATE check_in_records SET student_id=?, room_id=?, check_in_date=?, check_out_date=?, status=? WHERE check_in_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, checkIn.getStudentId());
            stmt.setInt(2, checkIn.getRoomId());
            stmt.setDate(3, new java.sql.Date(checkIn.getCheckInDate().getTime()));
            stmt.setDate(4, checkIn.getCheckOutDate() != null ? new java.sql.Date(checkIn.getCheckOutDate().getTime()) : null);
            stmt.setString(5, checkIn.getStatus());
            stmt.setInt(6, checkIn.getCheckInId());
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer checkInId) {
        String sql = "DELETE FROM check_in_records WHERE check_in_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, checkInId);
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public CheckIn findById(Integer checkInId) {
        String sql = "SELECT * FROM check_in_records WHERE check_in_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, checkInId);
            ResultSet rs = stmt.executeQuery();
            if (rs.next()) {
                return extractCheckInFromResultSet(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<CheckIn> findAll() {
        List<CheckIn> checkIns = new ArrayList<>();
        String sql = "SELECT * FROM check_in_records";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                checkIns.add(extractCheckInFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkIns;
    }

    @Override
    public List<CheckIn> findByStudentId(Integer studentId) {
        List<CheckIn> checkIns = new ArrayList<>();
        String sql = "SELECT * FROM check_in_records WHERE student_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, studentId);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                checkIns.add(extractCheckInFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkIns;
    }

    @Override
    public List<CheckIn> findByRoomId(Integer roomId) {
        List<CheckIn> checkIns = new ArrayList<>();
        String sql = "SELECT * FROM check_in_records WHERE room_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, roomId);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                checkIns.add(extractCheckInFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkIns;
    }

    @Override
    public List<CheckIn> findByCondition(String condition) {
        List<CheckIn> checkIns = new ArrayList<>();
        String sql = "SELECT * FROM check_in_records WHERE status LIKE ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, "%" + condition + "%");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                checkIns.add(extractCheckInFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return checkIns;
    }

    private CheckIn extractCheckInFromResultSet(ResultSet rs) throws SQLException {
        CheckIn checkIn = new CheckIn();
        checkIn.setCheckInId(rs.getInt("check_in_id"));
        checkIn.setStudentId(rs.getInt("student_id"));
        checkIn.setRoomId(rs.getInt("room_id"));
        checkIn.setCheckInDate(rs.getDate("check_in_date"));
        checkIn.setCheckOutDate(rs.getDate("check_out_date"));
        checkIn.setStatus(rs.getString("status"));
        return checkIn;
    }
} 